package StringBuilder;

/**
 * @author 王艺锡
 * @version 1.0
 */

//StringBuilder和StringBuffer基本是一样的，
// 只不过StringBuilder是用于单线程，而StringBuffer是用于多线程
public class StringBuilderIntroduce {
    public static void main(String[] args) {

        //String  StringBuffer  StringBuilder的对比
        //1.String:不可变字符序列，效率低，但是复用率高。如果对String做大量修改，不适用String。
        //2.StringBuffer:可变字符序列，效率较高(增删)，线程安全
        //3.StringBuilder:可变字序列，效率最高，线程不安全

        //三者效率对比
        long startTime = 0L;
        long endTime = 0L;



        //buffer
        StringBuffer buffer = new StringBuffer("");
        startTime = System.currentTimeMillis();
        for (int i = 0;i<100000;i++){
            buffer.append(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("buffer:" + (endTime - startTime));

        //builder
        StringBuilder builder = new StringBuilder("");
        startTime = System.currentTimeMillis();
        for (int i = 0;i<100000;i++){
        builder.append(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("builder:" + (endTime - startTime));


        //String
        String str = "";
        startTime = System.currentTimeMillis();
        for (int i = 0;i<100000;i++){
            str = str + i;
        }
        endTime = System.currentTimeMillis();
        System.out.println("String:" + (endTime - startTime));

    }
}
